import axios from "axios"
import { ElMessage } from 'element-plus'

const inst = axios.create({
    baseURL: 'http://demo11.5kcrm.net/index.php',
})

inst.defaults.headers.post["Content-Type"] = 'application/x-www-form-urlencoded'

inst.interceptors.request.use(config => (
    {...config, headers: {
        Authkey: '24afd9e4b45d7d7bed9fb95237d75ae9',
        Sessionid: 'ed31nfh4fb8723lb09ki8tc7c9'
    }}
))

inst.interceptors.response.use(response => {
    if(response.status !== 200) {
        ElMessage.error(response.statusText || '网络错误')
        return false
    }
    const { code, data, error } = response.data
    if(code !== 200) {
        ElMessage.error(error || '服务未知错误')
        // return false
        return Promise.reject(error)
    }
    return data
})

export const login = (username, password) => inst.post('/admin/base/login', {username, password})

export const report = ({dataType, type} = {dataType: 1, type: 'today'}) => inst.post('/crm/index/index', {dataType, type})